﻿using ProjScan.Modules;
using ProjScan.ScanGUI.Tools;
using ProjScan.Tools;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    //
    // 摘要:
    //     白名单窗口 - Class25
    public class WhiteWindow : EditorWindow
    {
        private string ruleName;

        private string ruleID;

        private List<string> list_0;

        protected Rect rect_0 = new Rect(20f, 20f, 400f, 500f);

        private Vector2 vector2_0;

        private GUIStyle guistyle_0;

        private GUIStyle guistyle_1;

        private Module moduleType;

        private void DrawWindow(int windowID)
        {
            GUI.BringWindowToFront(windowID);
        }

        private void DrawAdd()
        {
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            string text = null;
            GUIStyle gUIStyle = new GUIStyle(GUI.skin.button);
            gUIStyle.fontSize = 14;
            gUIStyle.fixedWidth = 75f;
            gUIStyle.alignment = TextAnchor.MiddleCenter;
            if (GUILayout.Button("添加[ + ]", gUIStyle))
            {
                text = EditorUtility.OpenFilePanel("添加目录", Environment.CurrentDirectory, "");
                if (string.IsNullOrEmpty(text))
                {
                    return;
                }

                text = ToolsHelper.ConvertAbsolutePathToRelative(text).Replace("%20", " ");
                if (!list_0.Contains(text))
                {
                    list_0.Add(text);
                    WhiteAssetManager.Instance.AddEntry(ruleID, text);
                }
            }

            GUILayout.EndHorizontal();
        }

        public void DrawPageInfo()
        {
            GUILayout.BeginHorizontal();
            Label_BigBold(" 规则：" + ruleName);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.Label("页面功能介绍:\n1. 保存在此页面中的白名单已增量保存在本地。\n2. 可从失败页面中加入资源到此白名单，也可点击“添加”选择白名单文件。", guistyle_1);
            GUILayout.EndHorizontal();
            GUITools.DrawColoredRectangle(base.position.width);
        }

        public void DrawPageMain(float width)
        {
            DrawRuleState.Initialize(2);
            GUITools.DrawStyledVerticalLayout(drawShadow: true);
            DrawWhiteListHeader();
            DrawWhiteList();
            DrawAdd();
            GUITools.EndVertical();
        }

        public void DrawWhiteList()
        {
            GUILayoutOption[] options = new GUILayoutOption[2]
            {
                GUILayout.Width(base.position.width - 20f),
                GUILayout.Height(base.position.height - 200f)
            };
            vector2_0 = EditorGUILayout.BeginScrollView(vector2_0, options);
            if (list_0 == null || list_0.Count == 0)
            {
                GUILayout.Label("List is Empty");
            }
            else
            {
                List<string> list = list_0;
                for (int i = 0; i < list_0.Count; i++)
                {
                    string text = list[i];
                    EditorGUILayout.BeginHorizontal();
                    GUILayoutOption[] options2 = new GUILayoutOption[1]
                    {
                        GUILayout.Width(base.position.width - 110f)
                    };
                    if (GUILayout.Button(text, guistyle_0, options2))
                    {
                        ToolsHelper.SelectionObject(text, moduleType);
                    }

                    GUILayout.FlexibleSpace();
                    GUIStyle gUIStyle = new GUIStyle(GUI.skin.button);
                    gUIStyle.fontSize = 14;
                    gUIStyle.fixedWidth = 75f;
                    gUIStyle.alignment = TextAnchor.MiddleLeft;
                    if (GUILayout.Button("移除[ - ]", gUIStyle))
                    {
                        list.RemoveAt(i);
                    }

                    EditorGUILayout.EndHorizontal();
                }

                list_0 = list;
                WhiteAssetManager.Instance.ReplaceRuleList(ruleID, list_0);
            }

            EditorGUILayout.EndScrollView();
            new GUITools.DirectoryListUI(ref list_0, "白名单", "", GUITools.DirectoryListUI.DrawOption.label);
        }

        public void DrawWhiteListHeader()
        {
            EditorGUILayout.BeginHorizontal();
            Label_BigBold("白名单列表");
            EditorGUILayout.EndHorizontal();
            GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
            GUILayoutOption[] options = new GUILayoutOption[1]
            {
                GUILayout.Width(base.position.width - 35f)
            };
            GUILayout.Label("资源位置", Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.Header), options);
            GUILayout.EndHorizontal();
        }

        public void init(Module moduleType, string ruleName, string ruleID)
        {
            this.ruleName = ruleName;
            this.ruleID = ruleID;
            this.moduleType = moduleType;
            base.titleContent.text = "白名单";
            UpdateWhiteList();
            setupGUIStyle();
        }

        public static void Label_BigBold(string txt)
        {
            GUIStyle gUIStyle = new GUIStyle();
            gUIStyle.fontSize = 1;
            gUIStyle.fontSize = 16;
            gUIStyle.normal.textColor = GUI.skin.label.normal.textColor;
            GUILayout.Label(txt, gUIStyle);
        }

        public void OnGUI()
        {
            rect_0 = GUI.Window(0, rect_0, DrawWindow, "Fixing Window");
            DrawPageInfo();
            DrawPageMain(rect_0.width);
        }

        private void setupGUIStyle()
        {
            guistyle_0 = Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.AssetBar);
            guistyle_1 = Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.InfoBox);
        }

        public void UpdateWhiteList()
        {
            list_0 = WhiteAssetManager.Instance.GetWhiteListForRuleId(ruleID);
        }
    }
}